package base;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import exception.MyException;
import util.OnClickUtil;
import util.TUtil;
import util.XPermissionUtils;

/**
 * Created by 刘智强 on 2017/4/6.
 * <p>
 * T-MVP模式的fragment的基类, 可根据自己的业务逻辑更改方法
 */

public abstract class BaseFragment<T extends BasePresenter, M extends BaseModel> extends Fragment implements BaseView, View.OnClickListener {

    public T presenter;
    public Activity mActivity;
    private View view = null;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mActivity = activity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        if (getLayoutResource() == 0) {
            try {
                throw new MyException("请先设置布局文件");
            } catch (MyException e) {
                e.printStackTrace();
            }
        } else {
            view = inflater.inflate(getLayoutResource(), container, false);
        }
        presenter = TUtil.getClazz(this, 0);

        if (presenter == null) {
            try {
                throw new MyException("presenter对象为null");
            } catch (MyException e) {
                e.printStackTrace();
            }
        }
        presenter.setVM(TUtil.getT(this, 1), this);
        initView(view);
        initListener();
        return view;
    }

    public abstract int getLayoutResource();

    public abstract void initView(View v);

    public abstract void initClick(View v);

    public abstract void initListener();

    @Override
    public void onClick(View v) {
        if (OnClickUtil.fastClick()) {
            initClick(v);
        }

    }

    /**
     * 给控件添加点击监听
     *
     * @param ids
     */
    public final void addOnClickListener(int... ids) {
        if (ids != null && ids.length > 0) {
            for (int id : ids) {
                if (id == 0) {
                    try {
                        throw new MyException("未找到ID为:" + id + "的控件");
                    } catch (MyException e) {
                        e.printStackTrace();
                    }
                }
                View v = view.findViewById(id);
                if (v != null) {
                    v.setOnClickListener(this);
                } else {
                    try {
                        throw new MyException("未找到ID为：" + id + "的view");
                    } catch (MyException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        XPermissionUtils.onRequestPermissionsResult(mActivity, requestCode, permissions, grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
